import request from "@/request/index";
import {decryptByCssd, encryptByCssd} from '@/utils/Sm4Util'
import {R} from "@/type/dto/R";
import lodash from "lodash";
import router from "@/router";

const error_no_res = (): string => {
    const r: R<any> = {code: -1, data: null, msg: '接口无返回!'}
    return JSON.stringify(r);
}
const error_unknown = (): string => {
    const r: R<any> = {code: -2, data: null, msg: '未知错误!'}
    return JSON.stringify(r);
}

function checkData(s: string) {
    let r = JSON.parse(s) as R<Object>;
    if (r && r.code) {

        if (r.code == 99) {
            router.push('/login');
            return false;
        }
    }
    return true;
}

export async function req_get(url: string, data: any, functionId: string = ''): Promise<string> {
    try {
        const params = lodash.pickBy(data, item => item);
        //params 为去掉为空或为null字段的对象
        console.log('get url', url);
        console.log('get req', params);
        const mapData = encryptByCssd(params);
        let str = '';
        mapData.forEach((value, key) => {
            str += `${key}=${value}&`;
        });
        if (str.length > 0) {
            str = str.substring(0, str.length - 1);
        }
        const url2 = `${url}?${str}`;
        console.log('url2', url2);
        const res = await request(<any>{
            url: url2,
            method: 'get',
            headers: {
                'Content-Type': 'application/json',
                'appid': 'assist',
                'functionId': functionId
            },
        });
        console.log('res', res);
        if (res == undefined) {
            return error_no_res();
        }
        const s = decryptByCssd(res?.data);
        console.log('s', s);
        checkData(s);
        return s;
    } catch (e) {
        return error_unknown();
    }

}

export async function req_put(url: string, data: any) {
    try {
        console.log('put :data', data);
        const mapData = encryptByCssd(data);
        console.log('encrypt :data', mapData);
        // let str = '';
        // mapData.forEach((value, key) => {
        //     str += `${key}=${value}&`;
        // });
        // if (str.length > 0) {
        //     str = str.substring(0, str.length - 1);
        // }
        // let jsonObject = {};
        const formData = new FormData();
        mapData.forEach((value, key) => {
            formData.append(key, value);
        });
        // let sendData = JSON.stringify(jsonObject);
        console.log('sendData', formData);


        const res = await request({
            url,
            method: 'put',
            data: formData,
            headers: {
                'Content-Type': 'application/json',
                'appid': 'assist'
            },
        });
        if (res == undefined) {
            return error_no_res();
        }
        const s = decryptByCssd(res?.data);
        console.log('s', s);
        checkData(s);
        return s;
    } catch (e) {
        return error_unknown();
    }
}

export async function req_post(url: string, data: any, functionId: string = '') {
    try {
        console.log('post :data', data);
        const mapData = encryptByCssd(data);
        console.log('encrypt :data', mapData);
        // let str = '';
        // mapData.forEach((value, key) => {
        //     str += `${key}=${value}&`;
        // });
        // if (str.length > 0) {
        //     str = str.substring(0, str.length - 1);
        // }
        // let jsonObject = {};
        const formData = new FormData();
        mapData.forEach((value, key) => {
            formData.append(key, value);
        });
        // let sendData = JSON.stringify(jsonObject);
        console.log('sendData', formData);


        const res = await request({
            url,
            method: 'post',
            data: formData,
            headers: {
                'Content-Type': 'application/json',
                'appid': 'assist',
                'functionId': functionId
            },
        });
        console.log('res', res);
        if (res == undefined) {
            return error_no_res();
        }
        const s = decryptByCssd(res?.data);
        console.log('s', s);
        checkData(s);
        return s;
    } catch (e) {
        return error_unknown();
    }
}

export async function req_upload(url: string, formData: FormData) {
    try {
        const res = await request({
            url,
            method: 'post',
            data: formData,

            headers: {
                'Content-Type': 'multipart/form-data',
                'appid': 'assist'
            },
        });
        console.log('res', res);
        if (res == undefined) {
            return error_no_res();
        }
        const s = decryptByCssd(res?.data);
        console.log('s', s);
        checkData(s);
        return s;
    } catch (e) {
        return error_unknown();
    }
}


export async function req_delete(url: string, data: any) {
    try {
        console.log('delete :data', data);
        const mapData = encryptByCssd(data);
        console.log('encrypt :data', mapData);
        const formData = new FormData();
        mapData.forEach((value, key) => {
            formData.append(key, value);
        });

        let str = '?';
        mapData.forEach((value, key) => {
            str += `${key}=${value}&`;
        });
        if (str.length > 0) {
            str = str.substring(0, str.length - 1);
        }
        // let sendData = JSON.stringify(jsonObject);
        console.log('sendData', formData);
        url += str;

        const res = await request({
            url,
            method: 'delete',
            data: formData,
            headers: {
                'Content-Type': 'application/json',
                'appid': 'assist'
            },
        });
        console.log('res', res);
        if (res == undefined) {
            return error_no_res();
        }
        const s = decryptByCssd(res?.data);
        //这里需要转json
        checkData(s);

        console.log('s', s);
        return s;
    } catch (e) {
        return error_unknown();
    }
}



